package com.iweb.xt.sso.service.impl;

import com.iweb.xt.common.model.CallResult;
import com.iweb.xt.common.service.AbstractTemplateAction;
import com.iweb.xt.sso.domain.LoginDomain;
import com.iweb.xt.sso.domain.repository.LoginDomainRepository;
import com.iweb.xt.sso.model.param.login.LoginParam;
import com.iweb.xt.sso.service.LoginService;
import org.apache.commons.codec.digest.DigestUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl extends AbstractService implements LoginService {


//    依赖于loginDomainRepository
    @Autowired
    private LoginDomainRepository loginDomainRepository;
    @Override
    public CallResult getQRCodeUrl() {
        //根据repo 生成domain
       LoginDomain loginDomain= loginDomainRepository.createLoginDomain(new LoginParam());

        return this.serviceTemplate.executeQuery(new AbstractTemplateAction<String>() {
            @Override
            public CallResult<String> doAction() {
                return loginDomain.buildQRCodeUrl();
            }
        });
    }

    @Override
    public CallResult wxLoginCallBack(LoginParam loginParam) {
        LoginDomain loginDomain = loginDomainRepository.createLoginDomain(loginParam);
        return this.serviceTemplate.execute(new AbstractTemplateAction<Object>() {
            @Override
            public CallResult<Object> checkParam() {
//                防止csrf攻击 所以要先检查state  和我传入的是否一致
                return loginDomain.checkState();
            }

            @Override
            public CallResult<Object> doAction() {
                return loginDomain.login();
            }
        });
    }
}
